Thread: cin >> char[]

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    cin >> char[]

    I dont think this is possible but can I use cin to get whats inside a char[]? could also use string..

    this is gonna be used in a parser so I need to take one char at a time..

    ex.
    char* temp_string = "2*bob";
    char ch;
    cin.get(ch) >> temp_string;

    the parser should see this as 2 times bob..
    I know this exemple doesnt work... its just a guess what it might look like..
    thx
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "I dont think this is possible but can I use cin to get whats inside a char[]"

    cin operates on an input "stream", which is an abstract representation of an input device that's a source for data in your program--not on variables.

    You can access the characters in a char[] or a char* using array notation,

    char text[30]="2*bob";
    char letter;
    letter = text[0];

    or

    char* pstring="2*bob";
    char letter;
    letter = pstring[0];
    Last edited by 7stud; 04-04-2003 at 01:29 AM.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: cin >> char[]

    Originally posted by Luigi
    I dont think this is possible but can I use cin to get whats inside a char[]? could also use string..
    Good idea! This is possible and very useful.

    The solution is stringstream:

    Code:
    #include <sstream>
    using namespace std;
    
    string s = "2 3 4";
    stringstream strin(s);
    
    int a;
    //Use strin like cin
    strin >> a;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    std::istringstream is likely what you want, though if you want to parse "2*a" the streams provide no convenent way to pull off just the "2", then "*", etc... Due to the lack of whitespace. The boost spirit parser is probably the best, most elegant way to solve these sorts of problems, though it takes forever to compile. std::string and the find_first_of family of functions is probably the easiest way to go about these things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin >> string // multiple lines
    By msshapira in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2009, 04:39 AM
  2. cin >>
    By TehClutchKiller in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2008, 02:28 AM
  3. if (cin >> a) {
    By manzoor in forum C++ Programming
    Replies: 1
    Last Post: 02-24-2008, 07:48 AM
  4. cin >> something
    By junkeat90 in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2008, 05:45 AM
  5. cin >> buf endless loop
    By cfriend in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 04:01 PM